﻿@echo off
:: 设置控制台编码为UTF-8
chcp 65001 > nul
setlocal enabledelayedexpansion

:: ========================
:: WPF双应用发布简化版
:: ========================

:: 1. 基础配置
set "APP_NAME=极片分级-v2.0.1-Test"
set "SCRIPT_DIR=%~dp0"
set "MAIN_PROJECT=%SCRIPT_DIR%..\WpfApp\WpfApp.csproj"
set "LAUNCHER_PROJECT=%SCRIPT_DIR%..\Launcher\Launcher.csproj"
set "UPDATE_PROJECT=%SCRIPT_DIR%..\Update\Update.csproj"
set "PUBLISH_ROOT=%SCRIPT_DIR%bin\publish\%APP_NAME%"
set "CONFIGURATION=Debug"
set "RUNTIME=win-x86"

:: 2. 验证项目文件
if not exist "%MAIN_PROJECT%" (
    echo [错误] 主项目文件不存在
    pause
    exit /b 1
)

if not exist "%LAUNCHER_PROJECT%" (
    echo [错误] 启动器项目文件不存在
    pause
    exit /b 1
)

if not exist "%UPDATE_PROJECT%" (
    echo [错误] 更新程序项目文件不存在
    pause
    exit /b 1
)

:: 3. 清理旧版本
if exist "%PUBLISH_ROOT%" (
    echo 正在清理旧版本...
    rmdir /s /q "%PUBLISH_ROOT%" 2>nul
)

:: 4. 发布主程序
echo.
echo 正在发布主程序...
dotnet publish "%MAIN_PROJECT%" -c %CONFIGURATION% -r %RUNTIME% -o "%PUBLISH_ROOT%\main" --no-self-contained

if errorlevel 1 (
    echo [错误] 主程序发布失败
    pause
    exit /b 1
)

:: 5. 发布启动器
echo.
echo 正在发布启动器...
dotnet publish "%LAUNCHER_PROJECT%" -c %CONFIGURATION% -r %RUNTIME% -o "%PUBLISH_ROOT%" --self-contained

if errorlevel 1 (
    echo [错误] 启动器发布失败
    pause
    exit /b 1
)

:: 6. 发布更新程序
echo.
echo 正在发布更新程序...
dotnet publish "%UPDATE_PROJECT%" -c %CONFIGURATION% -r %RUNTIME% -o "%PUBLISH_ROOT%\update" --no-self-contained

if errorlevel 1 (
    echo [错误] 更新程序发布失败
    pause
    exit /b 1
)

:: 7. 移动共享资源
echo.
echo 正在整理共享资源...
if exist "%PUBLISH_ROOT%\main\Assets" (
    move /Y "%PUBLISH_ROOT%\main\Assets" "%PUBLISH_ROOT%\Assets" >nul
    echo ✓ Assets已移动到根目录
)

if exist "%PUBLISH_ROOT%\main\appsettings.json" (
    move /Y "%PUBLISH_ROOT%\main\appsettings.json" "%PUBLISH_ROOT%\" >nul
    echo ✓ appsettings.json已移动到根目录
)

:: 8. 完成提示
echo.
echo ========== 发布成功 ==========
echo 主程序位置: %PUBLISH_ROOT%\main\
echo 启动器位置: %PUBLISH_ROOT%\
echo 更新程序位置: %PUBLISH_ROOT%\
echo 生成时间: %DATE% %TIME%
echo =============================
echo.

:: 9. 打开资源管理器
start "" explorer "%PUBLISH_ROOT%"

pause
exit /b 0